package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.HotSearchMapper;
import com.wenwan.manage.pojo.HotSearch;
import com.wenwan.manage.service.HotSearchService;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class HotSearchServiceImpl implements HotSearchService {

	@Autowired
	private HotSearchMapper hotSearchMapper;

	@Override
	public Map<String, Object> create(String name, Integer times) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (name != null && name.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", DigestUtils.md5Hex(name).toUpperCase());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("name", name);
		map.put("times", times);

		Boolean data = hotSearchMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> update(String id, String name, Integer times) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && id.length() != 32) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_LENGTH_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_LENGTH_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (name != null && name.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("name", name);
		map.put("times", times);
		map.put("updateTime", Util.getTime());

		Boolean data = hotSearchMapper.update(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		// DataToObject<List<HotSearch>> dto = new DataToObject<List<HotSearch>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);

		List<HotSearch> data = hotSearchMapper.selectAll(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
